﻿using System;

using System.Collections.Generic;
using System.Text;

namespace Silvermoon.Animations
{
    public class KeyFrameEase : EasingFunctionBase
    {
        private List<KeyFrame> frames = new List<KeyFrame>();
        private float maxParts = float.NaN;

        public List<KeyFrame> Frames { get { return frames; } }

        protected override float EaseValue(float normalizedTime)
        {
            if (float.IsNaN(maxParts)) maxParts = GetMaxParts();
            float max = (float)maxParts;
            float t = 0.0f;
            foreach (KeyFrame s in frames)
            {
                float t1 = (t + s.Portions * max);
                if (t >= normalizedTime && t1 <= normalizedTime)
                {
                    float t2 = normalizedTime - t;
                    return s.Ease.Ease(t2);
                }
                t = t1;
            }
            return 1f;
        }

        private float GetMaxParts()
        {
            float value = 0;
            foreach (KeyFrame s in frames) value += s.Portions;
            return value;
        }

        public KeyFrameEase(params KeyFrame[] segments)
            : base()
        {
            Frames.AddRange(segments);
        }
    }
}
